﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;

namespace FantastPlatform.Framework.Common
{
    public static class MessageManager
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["DXConnectionString"].ConnectionString;
        public static void SendMessage(string srcNumber, string message, string policeNum,DateTime sendTime)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                long destNumber = 0;
                int count = 0;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "select [DestNumber] from  [DXServer].[dbo].[DXSend] order by [DestNumber] Desc";
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        destNumber = Int64.Parse(reader[0].ToString());
                    }
                    reader.Close();
                }
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "select count(*) from  [DXServer].[dbo].[DXSend]";
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        count = Int32.Parse(reader[0].ToString());
                    }
                    reader.Close();
                }
                using (SqlCommand cmd2 = new SqlCommand())
                {
                    cmd2.Connection = conn;
                    destNumber = destNumber + 1;
                    string destNumberStr = destNumber.ToString("d6");
                    int id = count + 1;

                    cmd2.CommandText = "INSERT INTO [DXServer].[dbo].[DXSend] ([Id],[SrcNumber],[DestNumber],[Message],[Used],[WriteTime],[SendFlag],[PoliceNumber]) VALUES('" + id + "','" + srcNumber + "','" + destNumberStr + "','" + message + "'," + 1 + ",'" + sendTime + "'," + 0 + ",'" + policeNum + "')";
                    cmd2.ExecuteNonQuery();
                }
                conn.Close();
            }
        }
    }
}
